package com.atomos.strategy.emission
{
	import flash.geom.Point;
	
	import com.atomos.particle.MockParticle;
	import com.atomos.particle.Particle;
	
	import flexunit.framework.TestCase;	

	public class ByRateEmissionTest extends TestCase
	{
		protected var _emission : ByRateEmission;
		
		public override function setUp():void 
		{
			_emission = new ByRateEmission ( Particle, new Point(), 100 );
		}
		
		public function testConstruct() : void
		{
			var errorOccurs : Boolean;
			var emission : ByRateEmission;
			
			assertNotNull ( "Constructor failed to create the instance", _emission );
			
			errorOccurs = false;
			try
			{
				emission = new ByRateEmission ( null );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			assertTrue( "Constructor don't failed when passing a null Particle's class", errorOccurs );
			
			errorOccurs = false;
		
			try
			{
				emission = new ByRateEmission ( Point );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			assertTrue( "Constructor don't failed when passing an invalid Particle's class", errorOccurs );
		
			emission = new ByRateEmission ( Particle, new Point(), NaN );
			assertEquals( emission + " constructor failed to set a valid rate when passing NaN", 1, emission.getRate() );
			
			emission = new ByRateEmission ( Particle, new Point(), 0 );
			assertEquals( emission + " constructor failed to set a valid rate when passing 0", 1, emission.getRate() );
			 
		}
		public function testEmission () : void
		{
			var n : Number = -1;
			var t : Number;
			var p : Particle;
			
			_emission.prepareEmission( 100 );
			
			while ( _emission.hasNext() && ++n < 50 )
			{
				t = _emission.nextTime();
				p = _emission.next() as Particle;
				
				assertEquals ( _emission + ".nextTime() don't return the current timeStep value", 90 - n * 10, t ); 
				assertNotNull ( _emission + ".next() return a null particle during iteration " + n, p );
			}
			assertEquals ( _emission + ".hasNext() failed to realize the right number of iteration with a step of 100", 9, n );
			
			
			n = -1;
			var pcount : Number = 0;
			while ( ++n < 10 )
			{
				_emission.prepareEmission( 1 );
				while ( _emission.hasNext() )
				{
					t = _emission.nextTime();
					p = _emission.next() as Particle;
					
					assertEquals ( _emission + ".nextTime() don't return the current timeStep value", 0, t ); 
					assertNotNull ( _emission + ".next() return a null particle", p );
					pcount++;
				}
			}
			assertEquals ( _emission + ".hasNext() failed to realize the right number of iteration with a step of 1", 10, n );
			assertEquals ( _emission + ".hasNext() don't create the right number of particles with a step of 1", 1, pcount );
		}
		public function testGetSetRate () : void
		{
			_emission.setRate( 20 );
			assertEquals( _emission + ".setRate() failed to modify the rate when passing 20", 20, _emission.getRate() );
			
			_emission.setRate( NaN );
			assertEquals( _emission + ".setRate() failed to modify the rate when passing NaN", 1, _emission.getRate() );
			
			_emission.setRate( 0 );
			assertEquals( _emission + ".setRate() failed to modify the rate when passing 0", 1, _emission.getRate() );
		}
		
		public function testGetSetType () : void
		{
			var errorOccurs : Boolean;
			
			_emission.setParticleType( MockParticle );
			
			assertEquals ( _emission + ".setParticleType() failed with a valid Particle class", MockParticle ,_emission.getParticleType() );
			
			errorOccurs = false;
			try
			{
				_emission.setParticleType( null );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			assertTrue( _emission + ".setParticleType() don't failed when passing a null Particle's class", errorOccurs );
			
			errorOccurs = false;
		
			try
			{
				_emission.setParticleType ( Point );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			assertTrue( "Constructor don't failed when passing an invalid Particle's class", errorOccurs );
		}
		
		public function testGetSetPosition () : void
		{
			assertNotNull ( _emission + " constructor failed to defined a default position", _emission.getPosition() );
			
			_emission.setPosition( null );
			
			assertNotNull ( _emission + ".setPosition() failed with null as argument", _emission.getPosition() );
			
			var p : Point = new Point( 5, 5 );
			_emission.setPosition( p );
			
			assertNotNull ( _emission + ".setPosition() failed with a valid point as argument", _emission.getPosition() );
			assertEquals ( _emission + ".getPosition() don't return the previously added point", p, _emission.getPosition() );			
		}
	}
}